<template>
    <div class="con">
        <div class="header">
            <p>为了保障您的资金安全</p>
            <p>您添加的所有银行卡必须为您本人所有</p>
        </div>

        <div class="info1">
            <dl>
                <dd>姓名</dd>
                <dt>
                    <input type="text" placeholder="请输入持卡人姓名" v-model="username">
                </dt>
            </dl>
            <dl>
                <dd>身份证号</dd>
                <dt>
                    <input
                        type="text"
                        v-model="idNumber"
                        placeholder="请输入持卡人身份证号码"
                        oninput="if(this.value.length>18) this.value = this.value.slice(0,18);"
                    >
                </dt>
            </dl>
            <dl>
                <dd>银行卡号</dd>
                <dt>
                    <input
                        type="text"
                        placeholder="请输入银行卡号"
                        @keyup="bankNumberCheck()"
                        @blur="bankNumberBulr()"
                        v-model="accountNo"
                        pattern="[0-9]*"
                        maxlength="23"
                    >
                </dt>
            </dl>
            <dl class="last">
                <dd>所属银行</dd>
                <dt>
                    <p>{{bankName}}</p>
                </dt>
            </dl>
        </div>
        <div class="info1" style="margin-top:12px">
            <dl>
                <dd>手机号</dd>
                <dt>
                    <input
                        type="text"
                        placeholder="请输入银行预留手机号"
                        pattern="[0-9]*"
                        v-model="phone"
                        oninput="if(this.value.length>11) this.value = this.value.slice(0,11);"
                    >
                </dt>
            </dl>
            <dl class="last">
                <dd>验证码</dd>
                <dt>
                    <input
                        type="text"
                        pattern="[0-9]*"
                        placeholder="请输入验证码"
                        style="margin-right:80px;width:132px"
                        v-model="authCode"
                    >
                    <button @click="send()" id="yzm">获取验证码</button>
                </dt>
            </dl>
        </div>

        <div class="btn" @click="ok()">
            <button>提交</button>
        </div>
    </div>
</template>

<script>
export default {
    name: "addsavecard",
    data() {
        return {
            username: "",
            idNumber: "",
            phone: "",
            accountNo: "",
            bankName: "",
            authCode: ""
        };
    },
    methods: {
        // 银行卡数字正则匹配
        bankNumberCheck() {
            this.accountNo = this.accountNo
                .replace(/\D/g, "")
                .replace(/(\d{4})(?=\d)/g, "$1 ");
        },
        bankNumberBulr() {
            this.axios({
                method: "post",
                url: `${
                    this.siteUrl
                }/app/bankCard/getBankInfo?appid=web.2019&format=json&timestamp=${
                    this.timestamp
                }`,
                data: {
                    userId: localStorage.getItem("userId"),
                    bankNo: this.accountNo.replace(/\s/g, ""),
                    cardType: 0
                }
            })
                .then(response => {
                    console.log(response);
                    if (response.data.success == 1) {
                        this.bankName = response.data.data.bankName;
                        this.bankId = response.data.data.id;
                    } else {
                        this.$toast(response.data.message);
                    }
                })
                .catch(error => {
                    console.log(error);
                    this.$toast("连接服务器异常");
                });
        },
        send() {
            var myreg = /^[1][0-9]{10}$/;
            if (this.username == "") {
                this.$toast("请输入持卡人姓名");
            } else if (this.idNumber == "") {
                this.$toast("请输入持卡人身份证号码");
            } else if (this.accountNo == "") {
                this.$toast("请输入银行卡号");
            } else if (this.bankName == "") {
                this.$toast("请输入正确的银行卡号获取所属银行");
            } else if (this.phone == "") {
                this.$toast("请输入手机号");
            } else if (!myreg.test(this.phone)) {
                this.$toast("请填写正确的手机号");
            } else {
                         var yzm = document.getElementById("yzm");

                var clock = "";
                var nums = 60;
                yzm.disabled = true; //将按钮置为不可点击
                yzm.style.opacity = "0.5";
                yzm.innerHTML = nums + "s";
                clock = setInterval(doLoop, 1000); //一秒执行一次
                function doLoop() {
                    nums--;
                    if (nums > 0) {
                        yzm.innerHTML = nums + "s";
                    } else {
                        clearInterval(clock); //清除js定时器
                        yzm.disabled = false;
                        yzm.style.opacity = "1";

                        yzm.innerHTML = "获取验证码";
                        nums = 60; //重置时间
                    }
                }
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/app/channel_1/getCode?appid=web.2019&format=json&timestamp=${
                        this.timestamp
                    }`,
                    data: {
                        authType: 4, // 和发送验证码里面的 authType 类似
                        phone: this.phone,
                        idNumber: this.idNumber,
                        accountName: this.username,
                        cardType: 0, // 0 储蓄卡 1.信用卡
                        bankId: this.bankId, //  根据卡号匹配银行接口会返回该数据
                        cardNo: this.accountNo.replace(/\s/g, ""),
                        userId: localStorage.getItem("userId")
                    }
                })
                    .then(response => {
                        console.log(response);
                        if (response.data.success == 1) {
                            this.$toast(response.data.message);
                        } else {
                            this.$toast(response.data.message);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        this.$toast("连接服务器异常");
                    });
            }
        },
        ok() {
            var myreg = /^[1][0-9]{10}$/;
            if (this.username == "") {
                this.$toast("请输入持卡人姓名");
            } else if (this.idNumber == "") {
                this.$toast("请输入持卡人身份证号码");
            } else if (this.accountNo == "") {
                this.$toast("请输入银行卡号");
            } else if (this.bankName == "") {
                this.$toast("请输入正确的银行卡号获取所属银行");
            } else if (this.phone == "") {
                this.$toast("请输入手机号");
            } else if (!myreg.test(this.phone)) {
                this.$toast("请填写正确的手机号");
            } else if (this.authCode == "") {
                this.$toast("请输入验证码");
            } else {
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/app/channel_1/updateSettleInfo?appid=web.2019&format=json&timestamp=${
                        this.timestamp
                    }`,
                    data: {
                        phone: this.phone,
                        cardType: 0, // 0 储蓄卡 1信用卡
                        accountName: this.username,
                        bankId: this.bankId,
                        idNumber: this.idNumber,
                        bankName: this.bankName,
                        userId: localStorage.getItem("userId"),
                        authCode: this.authCode,
                        accountNo: this.accountNo.replace(/\s/g, "")
                    }
                })
                    .then(response => {
                        console.log(response);
                        if (response.data.success == 1) {
                            this.$toast(response.data.message);
                            if (this.$route.params.type == 1) {
                                this.$router.push({ name: "credit" });
                            } else {
                                this.$router.push({ name: "mycard" });
                            }
                        } else {
                            this.$toast(response.data.message);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        this.$toast("连接服务器异常");
                    });
            }
        }
    },

    mounted() {
        this.axios({
            method: "post",
            url: `${
                this.siteUrl
            }/app/bankCard/getBankCardListByUserId?appid=web.2019&format=json&timestamp=${
                this.timestamp
            }`,
            data: {
                userId: localStorage.getItem("userId"),
                cardType: 0
            }
        })
            .then(response => {
                console.log(response);
                if (response.data.success == 1) {
                    if (response.data.data.length == 0) {
                        // this.show = true;
                    } else {
                        this.username = response.data.data[0].accountName;
                        this.idNumber = response.data.data[0].idNumber;
                        this.phone = response.data.data[0].accountPhone;
                        this.bankId = response.data.data[0].bankId;
                        this.bankName = response.data.data[0].bankName;
                        this.accountNo = response.data.data[0].accountNo;
                    }
                } else {
                    this.$toast(response.data.message);
                }
            })
            .catch(error => {
                console.log(error);
                this.$toast("连接服务器异常");
            });
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    width: 100%;
    min-height: 100vh;
    background: #f9f9f9;
    overflow: hidden;
}
.con .header {
    width: 100%;
    height: 62px;
    text-align: center;
    margin-top: 20px;
}
.con .header p {
    line-height: 22px;
    color: #a6a9b5;
    font-weight: 200;
    font-size: 14px;
}
.con .info1 {
    background: #fff;
}

.con .info1 dl {
    height: 60px;
    border-bottom: 1px solid #ececec;
    width: 90%;
    margin-left: 5%;
}
.con .info1 dd {
    line-height: 60px;
    color: #2d3659;
    font-weight: 200;
    float: left;
}
.con .info1 dt {
    float: right;
    width: 200px;
    height: 60px;
    line-height: 60px;
    position: relative;
    text-align: right;
    color: #2d3659;
}
.con .info1 button {
    position: absolute;
    right: -10px;
    top: 0px;
    color: #ffae4e;
    background: transparent
}
.con .info1 .last {
    border: none;
}
.con .info1 dt input {
    text-align: right;
    width: 200px;
}

.btn {
    width: 90%;
    height: 48px;
    line-height: 48px;
    background: #ffae4e;
    text-align: center;
    border-radius: 25px;
    box-shadow: 0px 10px 30px 0px #fbc27f;
    position: fixed;
    bottom: 40px;
    left: 5%;
}
.btn button {
    width: 100%;
    font-size: 17px;
    color: #fff;
    background: transparent;
}

input::-webkit-input-placeholder {
    color: #e9eaee;
    font-size: 14px;
}
</style>
